/*
 * Copyright (C) 2012 Evgeniy Egorov
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/*
 * FGinTools.java
 *
 * Created on Mar 20, 2012, 12:48:59 PM
 */
package ru.apertum.diasled.forms;

import com.jogamp.newt.event.KeyEvent;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JComponent;
import javax.swing.KeyStroke;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import ru.apertum.diasled.model.exam.GinCanvas;
import ru.apertum.diasled.model.exam.GinElemsTableModel;

/**
 *
 * @author Evgeniy Egorov
 */
public class FGinTools extends javax.swing.JDialog {

    public static FGinTools getInstance() {
        return FGinToolsHolder.INSTANCE;
    }

    private static class FGinToolsHolder {

        private static final FGinTools INSTANCE = new FGinTools(null, false);
    }

    /** Creates new form FGinTools */
    private FGinTools(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();

        // свернем по esc
        getRootPane().registerKeyboardAction(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                setVisible(false);
            }
        },
                KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0),
                JComponent.WHEN_IN_FOCUSED_WINDOW);


        sliderHorizontalScale.addChangeListener(new ChangeListener() {

            @Override
            public void stateChanged(ChangeEvent e) {
                canvas.setScaleX(sliderHorizontalScale.getModel().getValue());
            }
        });

        sliderVerticalScale.addChangeListener(new ChangeListener() {

            @Override
            public void stateChanged(ChangeEvent e) {
                canvas.setScaleY(sliderVerticalScale.getModel().getValue());
            }
        });


    }
    private GinCanvas canvas = null;

    public GinCanvas getCanvas() {
        return canvas;
    }

    public void setCanvas(GinCanvas canvas) {
        this.canvas = canvas;
        sliderVerticalScale.getModel().setValue(canvas.getScaleY());
        sliderHorizontalScale.getModel().setValue(canvas.getScaleX());

        tableGinElems.setModel(new GinElemsTableModel(canvas.getData()[0].length, canvas));
        canvas.setGinElems(((GinElemsTableModel) tableGinElems.getModel()).getElems());
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        sliderHorizontalScale = new javax.swing.JSlider();
        sliderVerticalScale = new javax.swing.JSlider();
        buttonVertScaleAuto = new javax.swing.JButton();
        buttonHorizScaleAuto = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tableGinElems = new javax.swing.JTable();

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(ru.apertum.diasled.Diasled.class).getContext().getResourceMap(FGinTools.class);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setAlwaysOnTop(true);
        setName("Form"); // NOI18N
        setResizable(false);
        setType(java.awt.Window.Type.UTILITY);

        sliderHorizontalScale.setMaximum(10000);
        sliderHorizontalScale.setMinimum(-100);
        sliderHorizontalScale.setValue(0);
        sliderHorizontalScale.setBorder(javax.swing.BorderFactory.createTitledBorder(resourceMap.getString("sliderHorizontalScale.border.title"))); // NOI18N
        sliderHorizontalScale.setName("sliderHorizontalScale"); // NOI18N

        sliderVerticalScale.setMaximum(500);
        sliderVerticalScale.setMinimum(-100);
        sliderVerticalScale.setValue(0);
        sliderVerticalScale.setBorder(javax.swing.BorderFactory.createTitledBorder(resourceMap.getString("sliderVerticalScale.border.title"))); // NOI18N
        sliderVerticalScale.setName("sliderVerticalScale"); // NOI18N

        buttonVertScaleAuto.setText(resourceMap.getString("buttonVertScaleAuto.text")); // NOI18N
        buttonVertScaleAuto.setName("buttonVertScaleAuto"); // NOI18N
        buttonVertScaleAuto.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonVertScaleAutoActionPerformed(evt);
            }
        });

        buttonHorizScaleAuto.setText(resourceMap.getString("buttonHorizScaleAuto.text")); // NOI18N
        buttonHorizScaleAuto.setName("buttonHorizScaleAuto"); // NOI18N
        buttonHorizScaleAuto.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonHorizScaleAutoActionPerformed(evt);
            }
        });

        jScrollPane1.setBorder(javax.swing.BorderFactory.createTitledBorder(resourceMap.getString("jScrollPane1.border.title"))); // NOI18N
        jScrollPane1.setName("jScrollPane1"); // NOI18N

        tableGinElems.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null},
                {null, null},
                {null, null},
                {null, null}
            },
            new String [] {
                "Title 1", "Title 2"
            }
        ));
        tableGinElems.setCellSelectionEnabled(true);
        tableGinElems.setName("tableGinElems"); // NOI18N
        tableGinElems.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jScrollPane1.setViewportView(tableGinElems);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 233, Short.MAX_VALUE)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(sliderVerticalScale, javax.swing.GroupLayout.DEFAULT_SIZE, 188, Short.MAX_VALUE)
                    .addComponent(sliderHorizontalScale, javax.swing.GroupLayout.DEFAULT_SIZE, 188, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(buttonHorizScaleAuto)
                    .addComponent(buttonVertScaleAuto))
                .addGap(0, 0, 0))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 113, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 30, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(buttonVertScaleAuto, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(sliderVerticalScale, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(sliderHorizontalScale, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(buttonHorizScaleAuto, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void buttonVertScaleAutoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonVertScaleAutoActionPerformed
        canvas.setDy(0);
        sliderVerticalScale.getModel().setValue(0);
    }//GEN-LAST:event_buttonVertScaleAutoActionPerformed

    private void buttonHorizScaleAutoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonHorizScaleAutoActionPerformed
        canvas.setDx(0);
        sliderHorizontalScale.getModel().setValue(0);
    }//GEN-LAST:event_buttonHorizScaleAutoActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton buttonHorizScaleAuto;
    private javax.swing.JButton buttonVertScaleAuto;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSlider sliderHorizontalScale;
    private javax.swing.JSlider sliderVerticalScale;
    private javax.swing.JTable tableGinElems;
    // End of variables declaration//GEN-END:variables
}
